<?php


namespace app\common\controller;

use app\common\model\SmsCodeModel;
use Qcloud\Sms\SmsSingleSender;

require CMF_ROOT . 'extend/qcloudsms_php/src/index.php';

class SmsController
{
    private static $config;

    public static function sendTxCode($phoneNumer, $params = ['3456'])
    {
        self::$config = config('txsms');
        try {
            $sender = new SmsSingleSender(self::$config['appid'], self::$config['appkey']);
            $result = $sender->sendWithParam('86', $phoneNumer, self::$config['templateId'], $params, self::$config['smsSign'], '', '');
            $content = json_decode($result, true);
            return $content;
        } catch (\Exception $e) {
            return $e;
        }

    }

    public static function sendAliCode($phoneNumbers = '18037371511', $code = '1235')
    {
//        self::$config = config('alisms');
        self::$config = cmf_get_option('sms_settings');
        $params = array();

        // *** 需用户填写部分 ***
        // fixme 必填：是否启用https
        $security = false;

        // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId = self::$config['accessKeyId'];
        $accessKeySecret = self::$config['accessKeySecret'];

        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = $phoneNumbers;

        // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $params["SignName"] = self::$config['SignName'];

        // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = self::$config['TemplateCode'];

        // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = array(
            "code" => $code,
        );

        // fixme 可选: 设置发送短信流水号
//        $params['OutId'] = "12345";

        // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段
//        $params['SmsUpExtendCode'] = "1234567";


        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }

        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new SignatureHelper();

        // 此处可能会抛出异常，注意catch
        $data = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action"   => "SendSms",
                "Version"  => "2017-05-25",
            )),
            $security
        );

        if ($data->Code == 'OK') {
            SmsCodeModel::create([
                'phone' => $phoneNumbers,
                'code'  => $code,
                'ip'    => request()->ip(),
            ]);
            session('phone_code', $code);
            return true;
        } else {
            return false;
        }
    }


}